package com.delivery.deliverysystem.controller;


import com.delivery.deliverysystem.common.ServerResponse;
import com.delivery.deliverysystem.pojo.DO.ProductCategory;
import com.delivery.deliverysystem.service.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品类别控制类
 */
@RestController
@RequestMapping("/productCategory")
@Api(tags = "产品类别接口")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @PostMapping("/createProductCategory")
    @ApiOperation(value = "创建产品类别",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse createProductCategory(@RequestBody ProductCategory category){
        boolean i = productCategoryService.createProductCategory(category);
        if(i) return ServerResponse.createBySuccess("创建成功");
        return ServerResponse.createByError("创建失败");
    }
    @PostMapping("/updateProductCategory")
    @ApiOperation(value = "修改产品类别",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse updateProductCategory(@RequestBody ProductCategory category){
        boolean i = productCategoryService.updateProductCategory(category);
        if(i) return ServerResponse.createBySuccess("更新成功");
        return ServerResponse.createByError("更新失败");
    }
    @PostMapping("/deleteProductCategory")
    @ApiOperation(value = "删除产品类别",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse deleteProductCategory(@RequestParam Integer id){
        boolean b = productCategoryService.removeById(id);
        if(b) return ServerResponse.createBySuccess("删除成功");
        return ServerResponse.createByError("删除失败");
    }
    @GetMapping("/getCategoryList")
    @ApiOperation(value = "获取全部产品类别",response = ServerResponse.class,httpMethod = "GET")
    public ServerResponse getCategoryList(){
        List<ProductCategory> categories = productCategoryService.getCategoryList();
        if(categories!=null && !categories.isEmpty()) return ServerResponse.createBySuccess(categories);
        return ServerResponse.createBySuccess("产品类别列表为空");
    }
    @GetMapping("/getCategoryById")
    @ApiOperation(value = "根据id查询产品类别",response = ServerResponse.class,httpMethod = "GET")
    public ServerResponse getCategoryById(@RequestParam Integer id){
        ProductCategory category = productCategoryService.getCategoryById(id);
        if(category==null) return ServerResponse.createByError("用户不存在");
        return ServerResponse.createBySuccess(category);
    }
}
